package com.jsmile.cloud.tradecenter.order.listener;

import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import com.jsmile.cloud.tradecenter.cart.CartApplicationService;
import com.jsmile.cloud.tradecenter.order.event.OrderCreateEvent;
import com.jsmile.cloud.tradecenter.order.model.OrderProduct;

import lombok.extern.slf4j.Slf4j;

/**
 * 订单创建--清除购物车
 */
@Slf4j
@Component
public class ClearCartListener {

    @Autowired
    private CartApplicationService cartApplicationService;

    @Async
    @Order
    @EventListener(OrderCreateEvent.class)
    public void orderStatusChange(OrderCreateEvent event) {
        log.info("订单创建清除购物车：{}", event.getOrder());
        try {
            cartApplicationService.deleteCart(event.getOrder().getUserId(), event.getOrder().getOrderProducts().stream().map(OrderProduct::getSkuId).collect(Collectors.toList()));
        } catch (Exception e) {

        }
    }
}
